package cn.com.huzo.service.financial.impl;

import cn.com.huzo.model.entities.financial.AccoutingPeriodEntity;
import cn.com.huzo.repository.financial.AccoutingPeriodRepository;
import cn.com.huzo.service.financial.AccoutingPeriodManager;
import huzofw.service.impl.GenericManagerImpl;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会计期管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("AccoutingPeriodManager")
public class AccoutingPeriodManagerImpl extends GenericManagerImpl<AccoutingPeriodEntity, String>
        implements AccoutingPeriodManager {

    @Resource(name = "AccoutingPeriodRepository")
    private AccoutingPeriodRepository _dao;

    /**
     * {@inheritDoc}
     *
     */
    @Transactional
    @Override
    public List<AccoutingPeriodEntity> fetchByApYear(String apYear) throws Exception {
        return _dao.fetchByApYear(apYear);
    }

    /**
     * {@inheritDoc}
     *
     */
    @Transactional
    @Override
    public Map<String, Object> fetchLastApYear() throws Exception {
        Map<String, Object> _result = new HashMap();
        String _lastApYear = _dao.fetchLastApYear();
        Calendar a = Calendar.getInstance();
        // 如果没有设置会计期，则默认返回上一年度，以利于用当前年度设置会计期
        _lastApYear = _lastApYear == null ? Integer.toString(a.get(Calendar.YEAR) - 1) : _lastApYear;
        _result.put("LastApYear", _lastApYear);

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     */
    @Transactional
    @Override
    public Integer bulk_insert(List<Map<String, Object>> itemList) throws Exception {
        return _dao.bulk_insert(itemList);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> saveLines(Map<String, ? extends List<AccoutingPeriodEntity>> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();
        int _rowsAffected1 = 0;
        int _rowsAffected2 = 0;
        String _apYear = null;

        if (criterion.get("insertList").size() != 0) {
            _apYear = criterion.get("insertList").get(0).getApYear();
            _rowsAffected1 = _dao.batchInsert(criterion.get("insertList"));
        }
        if (criterion.get("updateList").size() != 0) {
            _apYear = criterion.get("insertList").get(0).getApYear();
            _rowsAffected2 = _dao.batchUpdate(criterion.get("updateList"));
        }

        if (_rowsAffected1 > 0) {
            // 审计日志
            writeAuditLog(criterion.get("insertList").get(0).getUpdatedBy(),
                    "财务管理→会计期", "会计年度：" + criterion.get("insertList").get(0).getApYear(),
                    "增加");
        }
        if (_rowsAffected2 > 0) {
            // 审计日志
            writeAuditLog(criterion.get("updateList").get(0).getUpdatedBy(),
                    "财务管理→会计期", "会计年度：" + criterion.get("updateList").get(0).getApYear(),
                    "更新");
        }

        if ((_rowsAffected1 + _rowsAffected2) > 0) {
            rtnValue.put("LinesList", _dao.fetchByApYear(_apYear));
        }
        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();
        // 删除
        Integer rowsAffected = _dao.delete(MapUtils.getString(criterion, "ApYear"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "基础信息→会计期", "会计年度：" + MapUtils.getString(criterion, "ApYear"),
                    "删除");
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }
        return rtnValue;
    }
}
